package com.wanmait.baohan.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.wanmait.baohan.mapper.TravelApplicationMapper;
import com.wanmait.baohan.mapper.TravelApprovalMapper;
import com.wanmait.baohan.mapper.TravelPeopleMapper;
import com.wanmait.baohan.pojo.*;
import com.wanmait.baohan.service.TravelApplicationService;
import com.wanmait.baohan.util.AddDate;
import com.wanmait.baohan.util.CreatJobNumberUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * TODO
 *
 * @author hxn
 * @version 1.0
 * @date 2020/12/4 14:12
 * @description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TravelApplicationServiceImpl implements TravelApplicationService {
    @Resource
    private TravelApplicationMapper travelApplicationMapper;
    @Resource
    private TravelPeopleMapper travelPeopleMapper;
    @Resource
    private TravelApprovalMapper travelApprovalMapper;
    @Resource
    private HttpServletRequest request;

    /**
     *提交出差申请书信息
     *提交出差人员信息
     *提交审核信息
     * */
    @Override
    public void submitApplication(TravelApplication travelApplication) {
        List<TravelPeople> travelPeoples = new ArrayList<>();
        if (travelApplication.getTravelPeoples() != null){
            travelApplication.setTravelNumber(travelApplication.getTravelPeoples().size());
            travelApplication.getTravelPeoples().forEach(travelPeople -> travelPeople.setType(false));
            travelPeoples.addAll(travelApplication.getTravelPeoples());
        } else{
            travelApplication.setTravelNumber(0);
        }

        ProcessNode processNode = new ProcessNode();
        processNode.setNode(1);
        travelApplication.setProcessNumber(1);
        travelApplication.setState(1);
        travelApplication.setProcessNode(processNode);
        travelApplication.setAddTime(new Date());
        travelApplication.setPlanEndDate(AddDate.addDate(travelApplication.getPlanTravelDate(), travelApplication.getPlanDays()));
        Integer travelApplicationNumber = (Integer) request.getServletContext().getAttribute("travelApplicationNumbering");
        travelApplication.setNumbering(CreatJobNumberUtil.creatJobNumber(travelApplicationNumber));
        request.getServletContext().setAttribute("travelApplicationNumbering",++travelApplicationNumber);
        travelApplicationMapper.insert(travelApplication);

        TravelPeople applicationPeople = new TravelPeople();
        applicationPeople.setStaff(travelApplication.getStaff());
        applicationPeople.setType(true);
        travelPeoples.add(applicationPeople);
        travelPeoples.forEach(travelPeople -> travelPeople.setTravelApplicationId(travelApplication.getId()));
        travelPeopleMapper.insertList(travelPeoples);

        TravelApproval travelApproval = new TravelApproval();
        travelApproval.setState(1);
        travelApproval.setTravelApplication(travelApplication);
        travelApprovalMapper.insert(travelApproval);

    }

    /**
     * 获取全部我的出差申请
     * @param page
     * @param limit
     * @param travelApplication
     * @return
     */
    @Override
    public PageInfo<TravelApplication> selectMyApplication(Integer page, Integer limit, TravelApplication travelApplication, Date beginDate, Date endDate) {
        PageHelper.startPage(page, limit);
        List<TravelApplication> travelApplications = travelApplicationMapper.selectByStaffId(travelApplication, beginDate, endDate);
        PageInfo<TravelApplication> pageInfo = new PageInfo<>(travelApplications,7);
        return pageInfo;
    }

    @Override
    public PageInfo<TravelApplication> selectAll(Integer page, Integer limit, TravelApplication travelApplication, Date beginDate, Date endDate) {

        List<TravelApplication> travelApplications = travelApplicationMapper.selectAll(travelApplication, beginDate, endDate);
        for (TravelApplication application : travelApplications) {
            if (application.getProcessNode().getNode()==1){
                application.setStatus("部门经理审批");
            }
            else if(application.getProcessNode().getNode()==2){
                application.setStatus("运营审批");
            }else if(application.getProcessNode().getNode()==0){
                application.setStatus("未通过");
            } else if(application.getProcessNode().getNode()==3){
                application.setStatus("经理审批");
            }else if(application.getProcessNode().getNode()==4){
                application.setStatus("出差中");
            }else if(application.getProcessNode().getNode()==5){
                application.setStatus("出差结束");
            }else if(application.getProcessNode().getNode()==6){
                application.setStatus("出差总结");
            }else if(application.getProcessNode().getNode()==7){
                application.setStatus("出差完成");
            }else if(application.getProcessNode().getNode()==8){
                application.setStatus("申请报销");
            }else if(application.getProcessNode().getNode()==9){
                application.setStatus("报销完成");
            }
        }
        PageInfo<TravelApplication> pageInfo = new PageInfo<>(travelApplications,7);
        return pageInfo;
    }

    /**
     * 获取全部经审核的出差申请
     * @param page
     * @param limit
     * @param id
     * @return
     */
    @Override
    public PageInfo<TravelApplication> selectMyApplicationCarryOut(Integer page, Integer limit, Integer id) {
        PageHelper.startPage(page, limit);
        List<TravelApplication> travelApplications = travelApplicationMapper.selectByStaffIdCarryOut(1);
        PageInfo<TravelApplication> travelApplicationPageInfo = new PageInfo<>(travelApplications,7);
        return travelApplicationPageInfo;
    }

    /**
     * 出差申请驳回
     * @param id
     * @param rejectInfo
     */
    @Override
    public void rejectApplication(Integer id, String rejectInfo) {
        travelApplicationMapper.rejectApplicationByPrimaryKey(id);
    }

    @Override
    public List<TravelApplication> findAllApplicationCanBooking(Integer id) {
        return travelApplicationMapper.selectAllApplicationCanBooking(id);
    }

    @Override
    public void modifyNode(TravelApplication travelApplication) {
        travelApplicationMapper.updateByPrimaryKeySelective(travelApplication);
    }




    @Override
    public void insert(TravelApplication object) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public void update(TravelApplication object) {

    }

    /**
     * 通过出差申请的id获取申请信息
     * @param id
     * @return
     */
    @Override
    public TravelApplication findById(Integer id) {
        TravelApplication travelApplication = travelApplicationMapper.selectByPrimaryKey(id);
        return travelApplication;
    }

    @Override
    public List<TravelApplication> findAll() {
        return null;
    }

    @Override
    public List<TravelApplication> findAll(TravelApplication object) {
        return null;
    }

    @Override
    public PageInfo<TravelApplication> findPage(int pageNum) {
        return null;
    }

    @Override
    public PageInfo<TravelApplication> findPage() {
        return null;
    }

    @Override
    public PageInfo<TravelApplication> findPage(int pageNum, TravelApplication object) {
        return null;
    }

    @Override
    public PageInfo<TravelApplication> findPage(TravelApplication object) {
        return null;
    }
}
